<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="putio.js"></script>

<script>
$(document).ready(function(){
    /* These variables must be setup before running the tests */
    var API_KEY = "XXX";
    var API_SECRET = "XXX";
    var USER_NAME = "XXX";
    
    var FOLDER = "AFolder";
    var SUBFOLDER = "ASubFolder";
    
    var URLS = "http://www.clearbits.net/get/898-blue-ducks---six-ogg.torrent | http://www.clearbits.net/get/20-lawrence-lessig---free-culture.torrent";
    
    var api = putio(API_KEY, API_SECRET, function(error) {
        ok(false, error);
    });
    
    module("Test API methods");

    asyncTest("Verify that api object was correctly initialized", 4, function() {
        ok(api.getUserName() !== undefined, "User name was set-up correctly");
        ok(api.getUserName() == USER_NAME, "User name corresponds to '"+USER_NAME+"'");
        ok(api.access_token !== undefined, "User token was set with the object initialization");
        api.getUser()
            .when(function(user) {
                ok(user.name == USER_NAME, "User name returned by getUser is '"+USER_NAME+"'");
                start();
            });
    });
    
    
    if (confirm("This test will create a folder in your putio account in order "+
            "to test folder creation and downloading of files. "+
            "Do you wish to continue?")) {
        asyncTest("Test folder listing and creation methods", function() {
            api.createFolder(FOLDER)
                .when(function(folder) {
                    ok(folder.name = FOLDER, "Folder was was created successfully with name '"+FOLDER+"'");
                        
                    folder.createFolder(SUBFOLDER)
                        .when(function(subfolder) {
                            ok(subfolder.name = SUBFOLDER, "Sub-folder was was created successfully with name '"+SUBFOLDER+"'");
                            start();
                        });
                });
        });
    
        asyncTest("Find recently created folders and delete '"+FOLDER+"'", 3, function() {
            api.getFolderList()
                .beget(function(folders) {
                    var f;
                    for (var i = 0; i < folders.length; i++) {
                        if (folders[i].name == FOLDER) {
                            ok(true, "Found folder '"+FOLDER+"' with method getFolderList");
                            f = folders[i];
                        }
                            
                        if (folders[i].name == SUBFOLDER) {
                            ok(true, "Found folder '"+SUBFOLDER+"' with method getFolderList");
                        }
                    }
                        
                    return f;
                })
                .when(function(folder) {
                    folder.remove()
                        .when(function(success) {
                            ok(true, "Folder '"+folder.name+"' removed successfully");  
                            start();
                        });
                });
        });
    }
    
    asyncTest("Download files", function() {
            var bucket = api.createBucket();
            
            /* TODO: This is terrible. Too many sub-levels */
            bucket.extractUrls(URLS)
                .when(function(urls) {
                    ok(urls.length == 2, "2 Urls extracted correctly");
                        
                    bucket.analyze(urls)
                        .when(function(newBucket) {
                            ok(newBucket.getTorrentUrls().length == 2, 
                                "2 Urls, correctly analyzed");
                            
                            newBucket.fetch()
                                .when(function(transfers) {
                                    ok(transfers.length == 2, 
                                        "2 Transfers where correctly initialized");
                                    start();
                                });
                            }
                        );
                    }
                );
        });
        
    asyncTest("Cancel all transfers", 2, function() {
        api.getTransfers()
            .when(function(transfers) {
                var destroyed = 0;
                Iterator(transfers).each(function(index, transfer){
                    transfer.destroyTransfer()
                        .when(function(success) {
                            ok(true, "Correctly destroyed transfer with name '"+
                                transfer.name+"'");
                            
                            // TODO: Not very good practice, looks like it could
                            // cause a race condition
                            if (destroyed == transfers.length - 1) {
                                start();
                            }
                            destroyed++;
                        });
                });
            });
    });
    
});
</script>
  
</head>
<body>
    <h1 id="qunit-header">put.io library tests</h1>
    <h2 id="qunit-banner"></h2>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests"></ol>
    <div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
